package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQueryObject;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        //查询的时候多加一个数据,是否被软删除了
        List<Appointment> appointments = appointmentMapper.selectForList(qo);
        for (Appointment appointment : appointments) {
            appointment.setAppointmentTime(appointment.getAppointmentTime());
        }
        return new TablePageInfo<Appointment>(appointments);
    }
    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    @Override//保存
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        //新的预约单给默认状态预约中
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override//修改
    public void update(Appointment appointment) {
        Integer status = this.get(appointment.getId()).getStatus();
        //状态为已到店,用户取消或已结算的时候编辑不可修改
        if (Appointment.STATUS_ARRIVAL.equals(status)
        |Appointment.STATUS_CANCEL.equals(status)|Appointment.STATUS_SETTLE.equals(status)){
            throw new BusinessException("状态为已到店,用户取消或已结算的时候编辑不可修改");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override//删除
    public void deleteBatch(String ids) {

        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //状态为已到店,用户取消或已结算的时候不可删除
            Integer status = this.get(dictId).getStatus();
            if (Appointment.STATUS_ARRIVAL.equals(status)
                    |Appointment.STATUS_CANCEL.equals(status)|Appointment.STATUS_SETTLE.equals(status)){
                throw new BusinessException("状态为已到店,用户取消或已结算的时候不可删除");
            }
            //硬删除
//            appointmentMapper.deleteByPrimaryKey(dictId);
            //软删除
            appointmentMapper.delete(dictId,Appointment.DELETE_YES);
        }
    }



    @Override//取消预约
    public void cancel(Long id) {
        Integer status = this.get(id).getStatus();
        //状态为已到店,用户取消或已结算的时候编辑不可修改
        if (Appointment.STATUS_ARRIVAL.equals(status)
                |Appointment.STATUS_CANCEL.equals(status)|Appointment.STATUS_SETTLE.equals(status)){
            throw new BusinessException("状态为已到店,用户取消或已结算的时候不可取消");
        }
        appointmentMapper.cancel(id,Appointment.STATUS_CANCEL);
    }

    @Override//到店状态修改
    public void arrivalHandler(Long id) {
        Integer status = this.get(id).getStatus();
        //状态为已到店,用户取消或已结算的时候不可修改为到店
        if (Appointment.STATUS_ARRIVAL.equals(status)
                |Appointment.STATUS_CANCEL.equals(status)|Appointment.STATUS_SETTLE.equals(status)){
            throw new BusinessException("状态为已到店,用户取消或已结算的时候不可修改为到店");
        }

        Date date = new Date();

        appointmentMapper.arrivalHandler(id,Appointment.STATUS_ARRIVAL ,new Date());
    }


}
